package com.gaojun.tool;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * @author gj
 *
 * 2018年5月24日
 */
public class PropertiesUtil {
	 public static Properties prop = new Properties();
	    /**
	     * 取出值
	     * @param k
	     * @param filepath
	     * @return
	     */
	    public static String getValue(String k, String filepath){
	         InputStream in;
	        try {
	            in = new BufferedInputStream(new FileInputStream(filepath));
	            prop.load(in);     ///加载属性列表
	             Iterator<String> it=prop.stringPropertyNames().iterator();
	             while(it.hasNext()){
	                 String key=it.next();
	                 if (key.equals(k)){
	                     return prop.getProperty(key);
	                 }
	             }
	             in.close();
	        } catch (Exception e) {	        	
	            e.printStackTrace();
	        }  
	        return "";
	    }
	    /**
	     * 设置键值
	     * @param filepath
	     * @param map 
	     */
	    public static void setValue(String filepath, Map<String, String> map){
	        ///保存属性到b.properties文件
	        FileOutputStream oFile ;
	        try {
	            System.out.println("s"+filepath);
	            FileWriter writer = new FileWriter(filepath);
	           // oFile = new FileOutputStream(filepath, true);
	            //true表示追加打开
	           for (Entry<String, String> vo : map.entrySet()) {
	        	   prop.setProperty(vo.getKey(), vo.getValue());
		            //prop.put(map.get("key"), map.get("value"));		       
			    }
	           prop.store(writer, "The New properties file");
	           //oFile.close();
	           writer.close();
	        } catch (Exception e) {
	            // TODO 自动生成的 catch 块
	            e.printStackTrace();
	        }
	    }


}
